<template>
	<view class="tag-select" v-if="show">
		<view class="mark" @touchmove.stop="" @click="show = false"></view>

		<view class="content" :style="{ height: members.length > 1 ? '770rpx' : '600rpx' }">
			<view class="content-head"><view @click="show = false" class="content-head-touch"></view></view>
			<view class="content-title">
				<view class="title" v-if="title">{{ title }}</view>
			</view>

			<view class="tag-list" v-if="tags.length>1">
				<view :class="['tag-list-item', type == 1 ? 'act' : '']" @click="changeType(1)">按组对比</view>
				<view :class="['tag-list-item', type == 2 ? 'act' : '']" @click="changeType(2)">按人对比</view>
			</view>
			<view class="title-d" v-if="dtitle">{{ dtitle }}</view>
			<view class="content-tags" v-if="type == 1">
				<view :class="['tag', actList.indexOf(item.id) != -1 ? 'tag-act' : '']" @click="chooseItem(item, index)" v-for="(item, index) in tags" :key="index">
					{{ item[name] }}
				</view>
			</view>
			<view class="groups" v-if="type == 2">
				<view class="content-tags" v-for="group in tags" :key="group.id">
					<view class="content-tags-title" v-if="tags.length>1">{{ group.name }}</view>
					<view :class="['tag', actList.indexOf(item.id) != -1 ? 'tag-act' : '']" @click="chooseItem(item, index)" v-for="(item, index) in group.children" :key="index">
						{{ item[name] }}
					</view>
				</view>
			</view>

			<view class="content-btn"><view class="btn" @click="success">开始对比</view></view>
		</view>
	</view>
</template>

<script>
import { getMembers } from '@/apis/building.js';
export default {
	name: 'member-select',
	props: {
		members: {
			type: Array,
			default: () => {
				return [];
			}
		}
	},
	data() {
		return {
			type: 1, // 1按组 2按人
			keyval: '',
			title: '选择对比名单',
			dtitle: '至少选择1个选项',
			show: false,
			tags: [],
			actGroup: [],
			actMbs: [],
			actList: [],
			name: 'name'
		};
	},
	created() {
		if (this.members.length == 1) {
		} else {
		}

		this.tags = JSON.parse(JSON.stringify(this.members || []));
	},
	watch:{
		members() {
			this.tags = JSON.parse(JSON.stringify(this.members || []));
		}
	},
	methods: {
		changeType(type) {
			this.type = type;
			if (this.type == 1) {
				this.actList = this.actGroup;
			} else {
				this.actList = this.actMbs;
				// .slice(0,3);
			}
		},
		// 获取组及人员列表
		getGroups() {
			getMembers().then(res => {
				let msb = res.data;
				console.log('msb', msb);
			});
		},

		chooseItem(item, index) {
			const idx = this.actList.indexOf(item.id);
			if (idx == -1) {
				this.actList.push(item.id);
			} else {
				this.actList.splice(idx, 1);
			}
			console.log(this.tags);
		},
		init(data) {
			this.type = data.type;
			this.actGroup = data.groups;
			this.actMbs = data.mbs;
			this.keyval = data.key;
			if (this.type == 1) {
				this.actList = this.actGroup;
			} else {
				this.actList = this.actMbs;
			}
			this.show = true;
		},
		success() {
			let select = false;
		 
			if (this.actList.length == 0) {
				uni.showToast({
					title: '至少选择一项',
					icon: 'none'
				});
				return;
			}
			if (this.actList.length > 5) {
				uni.showToast({
					title: '最多选择5项',
					icon: 'none'
				});
				return;
			}
			this.$emit('success', {type:this.type, acts: this.actList, key: this.keyval });
			this.show = false;
		}
	}
};
</script>

<style scoped lang="scss">
.tag-select {
	width: 100%;
	position: fixed;
	top: 0;
	left: 0;
	z-index: 99;
	.mark {
		position: fixed;
		z-index: 99;
		background: rgba(0, 0, 0, 0.17);
		top: 0;
		left: 0;
		width: 100%;
		height: 100vh;
	}

	.tag-list {
		width: 100%;
		display: flex;
		align-items: center;
		font-size: 28rpx;
		border-bottom: 2rpx solid #eeeeee;
		&-item {
			margin-left: 40rpx;
			color: #333333;

			padding: 8rpx 0;
			border-bottom: 4rpx solid rgba(0, 0, 0, 0);
		}
		.act {
			font-weight: bold;
			border-bottom: 4rpx solid #0091ff;
		}
	}

	.content {
		z-index: 100;
		position: fixed;
		bottom: 0;
		left: 0;
		height: 600rpx;
		border-radius: 8rpx 8rpx 0rpx 0rpx;
		overflow: hidden;
		width: 100%;
		box-shadow: 0rpx -4rpx 12rpx 0rpx rgba(0, 0, 0, 0.12);
		background-color: #fff;
		animation: upm 0.3s;
		display: flex;
		flex-direction: column;
		&-head {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			height: 36rpx;
			&-touch {
				width: 64rpx;
				height: 10rpx;
				background: #d8d8d8;
				border-radius: 6rpx;
			}
		}

		&-title {
			width: calc(100% - 80rpx);
			line-height: 39rpx;
			padding: 20rpx 40rpx;
			padding-bottom: 0;
			.title {
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
				padding-bottom: 10rpx;
			}
		}
		.title-d {
			color: #999999;
			font-size: 24rpx;
			padding: 14rpx 0;
			padding-left: 40rpx;
		}
		.groups {
			width: 100%;
			flex: 1;
			overflow: scroll;
		}
		&-tags {
			flex: 1;
			overflow-y: scroll;
			width: calc(100% - 80rpx);
			padding: 0 40rpx;
			line-height: 67rpx;
			&-title {
				width: 100%;
				color: #999999;
				font-size: 24rpx;
				line-height: 40rpx;
				padding-top: 20rpx;
			}
			.tag {
				font-size: 28rpx;
				color: #666666;
				border: 2rpx solid #cccccc;
				height: 48rpx;
				text-align: center;
				display: inline-block;
				border-radius: 8rpx;
				line-height: 47rpx;
				padding: 0 22rpx;
				margin-right: 16rpx;
				transition: 0.1s all;
			}

			.tag-act {
				color: #0091ff;
				border-color: #0091ff;
			}
		}

		&-btn {
			width: 100%;
			.btn {
				width: 670rpx;
				height: 70rpx;
				background: #0091ff;
				border-radius: 8rpx;
				text-align: center;
				line-height: 69rpx;
				font-size: 28rpx;
				color: #fff;
				margin: 32rpx auto;
			}
		}
	}
}

@keyframes upm {
	0% {
		transform: translateY(870rpx);
	}

	100% {
		transform: translateY(0);
	}
}
</style>
